package com.air.ota.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.air.common.util.QueryCondition;
import com.air.common.util.QueryExpression;
import com.air.ota.mo.DepositTrans;
import com.air.ota.mo.InboundMsg;
import com.air.ota.mo.RegTrans;
import com.air.ota.service.IDepositResultHandlerService;
import com.air.ota.service.IDepositTransService;
import com.air.ota.service.IEpayService;
import com.air.ota.service.ISMGPService;
import com.air.ota.util.AppConstant;
import com.air.ota.util.HexStringUtil;
import com.air.ota.util.SZTInterface;
import com.air.ota.vo.SZTBaseResponseVO;
import com.air.ota.vo.SZTDepositResultReqTransVO;
import com.air.ota.vo.SZTDepositResultRequestVO;
import com.air.ota.vo.SZTDepositResultResTransVO;
import com.air.ota.vo.UTKDepositRequestUplinkVO;
import com.air.ota.vo.UTKDepositResultUplinkVO;

@Service("depositResultHandlerService")
public class DepositResultHandlerServiceImp extends BaseUplinkMsgHandler
	implements	IDepositResultHandlerService {
	
	@Autowired
	IDepositTransService depositTransService;
	@Autowired
	IEpayService epayService;
	@Autowired
	ISMGPService smgpService;
	
	@Override
	public void process(UTKDepositResultUplinkVO pkgVO, InboundMsg inboundMsg) throws Exception{
		System.out.println("------------Ready to Process Deposit Result");
		
		QueryCondition condition = new QueryCondition();
		condition.addQueryCondition("depositSequence",HexStringUtil.bytesToHexString(pkgVO.getDepositSequence()) , QueryExpression.EQUALS);
		condition.addQueryCondition("cellphone",inboundMsg.getPhoneNumber() , QueryExpression.EQUALS);
		List<DepositTrans> result= depositTransService.queryByCondition(condition);
		DepositTrans trans = result.get(0);
		
		if (trans.getResult() == AppConstant._depositResult_sztResult_success){
			System.out.println("--------------------Duplicate Deposit Result Uplink");
			return;
		}
		
		if (!HexStringUtil.bytesToHexString(pkgVO.getResult()).equals("9000")){
			htkResultFailureHandler(trans,pkgVO);
			return;
		}
		
		trans.setResult(AppConstant._depositResult_utkResult_success);
		trans.setTac(HexStringUtil.bytesToHexString(pkgVO.getTac()));
		trans.setEwalletRemain(HexStringUtil.bytesToHexString(pkgVO.getRemain()));
		trans.setCardTransSequence(HexStringUtil.bytesToHexString(pkgVO.getCardTransSequence()));
		depositTransService.updateById(trans);
		
		unlockRetryOutboundMsg(trans.getId(),AppConstant._outbound_msg_type_depositResponse);
		
		//调用深圳通接口
		byte[] deviceSequence = HexStringUtil.hexStringToBytes(trans.getDeviceSequence());
		byte[] sztSequence = HexStringUtil.hexStringToBytes(trans.getSztId());
		byte[] cardAppSequence = HexStringUtil.hexStringToBytes(trans.getCardAppSequence());
		byte[] depositResult = HexStringUtil.hexStringToBytes("00000000");
		
		SZTDepositResultReqTransVO sztDepositResultReqTransVO = new SZTDepositResultReqTransVO(
						deviceSequence,sztSequence,trans.getDeviceTriggerTime(),
						HexStringUtil.hexStringToBytes(trans.getCardAppSequence()),
						depositResult,
						HexStringUtil.hexStringToBytes(trans.getTac()),
						HexStringUtil.hexStringToBytes(trans.getEwalletRemain()),
						HexStringUtil.hexStringToBytes(trans.getCardTransSequence())
					);
		
		SZTDepositResultRequestVO sztDepositResultRequestVO = new SZTDepositResultRequestVO(sztDepositResultReqTransVO.getTransDataHexString());
		
		SZTInterface sztInterface = new SZTInterface();
		byte[] response=sztInterface.sendRequest(HexStringUtil.hexStringToBytes(sztDepositResultRequestVO.getMsgPackage()));
		SZTBaseResponseVO responseVO = new SZTBaseResponseVO();
		responseVO.parse(response);
		
		SZTDepositResultResTransVO depositResultResTransVO = new SZTDepositResultResTransVO();
		depositResultResTransVO.parse(HexStringUtil.hexStringToBytes(responseVO.getData()));
		
		if (depositResultResTransVO.getTransResult()==0x00){//Success
			System.out.println("success");
			trans.setResult(AppConstant._depositResult_sztResult_success);
			depositTransService.updateById(trans);
		}else{
			System.out.println("failure code="+HexStringUtil.byteToHexString(depositResultResTransVO.getTransResult()));
			sztResultFailureHandler(trans,pkgVO);
			return;
		}
		int amountYuan = Integer.parseInt(trans.getAmount(),16)/100;
		String prompt = "天翼手机深圳通充值成功！您本次充值"+amountYuan+"元";
		smgpService.sendTxtSMS(trans.getCellphone(), prompt);
		
	}
	
	private void htkResultFailureHandler(DepositTrans trans,UTKDepositResultUplinkVO pkgVO) throws Exception{
		if (trans.getResult()>0){
			epayService.refund(trans);
		}
		trans.setResult(AppConstant._depositResult_utkResult_failure);
		depositTransService.updateById(trans);
	}
	
	private void sztResultFailureHandler(DepositTrans trans,UTKDepositResultUplinkVO pkgVO) throws Exception{
		if (trans.getResult()>0){
			epayService.refund(trans);
		}
		trans.setResult(AppConstant._depositResult_sztResult_failure);
		depositTransService.updateById(trans);
	}
	
}
